package org.momonga1banchi.reminder.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {

//    public static void main(String[] args) throws Exception {
//        Calendar cal = DateUtil.getDate("１２011015５5");
//        int year = cal.get(Calendar.YEAR); // (2)現在の年を取得
//        int month = cal.get(Calendar.MONTH) + 1; // (3)現在の月を取得
//        int day = cal.get(Calendar.DATE); // (4)現在の日を取得
//        int hour = cal.get(Calendar.HOUR_OF_DAY); // (5)現在の時を取得
//        int minute = cal.get(Calendar.MINUTE); // (6)現在の分を取得
//        int second = cal.get(Calendar.SECOND); // (7)現在の秒を取得
//
//        System.out.println(year + "/" + month + "/" + day + " " + " " + hour + ":" + minute + ":" + second);
//
//    }

    public static Calendar getDate(String input) throws ParseException {
        Pattern datePattern = Pattern.compile("([0-9０-９]{10})");
        Matcher dateMatcher = datePattern.matcher(input);
        Pattern timePattern = Pattern.compile("([0-9０-９]{4})");
        Matcher timeMatcher = timePattern.matcher(input);

        String date = null;
        String time = null;
        if (dateMatcher.find()) {
            date = dateMatcher.group(1);
            date = date.replaceAll("０", "0").replaceAll("１", "1").replaceAll("２", "2").replaceAll("３", "3").replaceAll(
                    "４", "4").replaceAll("５", "5").replaceAll("６", "6").replaceAll("７", "7").replaceAll("８", "8")
                    .replaceAll("９", "9");
        } else if (timeMatcher.find()) {
            time = timeMatcher.group(1);
            time = time.replaceAll("０", "0").replaceAll("１", "1").replaceAll("２", "2").replaceAll("３", "3").replaceAll(
                    "４", "4").replaceAll("５", "5").replaceAll("６", "6").replaceAll("７", "7").replaceAll("８", "8")
                    .replaceAll("９", "9");
        }

        TimeZone tokyoTime = TimeZone.getTimeZone("JST");
        Calendar calendar = Calendar.getInstance(tokyoTime);
        // 色々やったけどだめだったので、とりあえず９時間進める。。
        calendar.add(Calendar.HOUR_OF_DAY, 9);

        // ||||||||||||||||||
        int year = calendar.get(Calendar.YEAR); // (2)現在の年を取得
        int month = calendar.get(Calendar.MONTH) + 1; // (3)現在の月を取得
        int day = calendar.get(Calendar.DATE); // (4)現在の日を取得
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // (5)現在の時を取得
        int minute = calendar.get(Calendar.MINUTE); // (6)現在の分を取得
        int second = calendar.get(Calendar.SECOND); // (7)現在の秒を取得
//        MyLog.log.info(year + "/" + month + "/" + day + " " + " " + hour + ":" + minute + ":" + second);
        System.out.println(year + "/" + month + "/" + day + " " + " " + hour + ":" + minute + ":" + second);

        // ||||||||||||||||||

        if (input == null || input.equals("")) {
            return calendar;
        } else {
            if (date != null) {
                DateFormat dbDateformat = new SimpleDateFormat("yyMMddHHmm");
                Date selectedDate = dbDateformat.parse(date);
                calendar.setTime(selectedDate);
            } else if (time != null) {
                DateFormat dbDateformat = new SimpleDateFormat("yyMMddHHmm");
                DecimalFormat formatter2d = new DecimalFormat("00");
                Date selectedTimeOnly = dbDateformat.parse(formatter2d.format(calendar.get(Calendar.YEAR) - 2000)
                        + formatter2d.format(calendar.get(Calendar.MONTH) + 1)
                        + formatter2d.format(calendar.get(Calendar.DAY_OF_MONTH)) + time);
//                MyLog.log.info(selectedTimeOnly.toString());
                calendar.setTime(selectedTimeOnly);
            }
        }
        return calendar;
    }
}
